package com.elitect.ftptransfer.module.main.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.elitect.ftptransfer.R;
import com.elitect.ftptransfer.module.main.data.FileItem;
import com.elitect.ftptransfer.util.Utils;

public class ListFilesAdapter extends BaseAdapter
{
    private List<FileItem> mFiles;
    private Context mContext;

    private int mResourceFile;
    private int mResourceFolder;

    private class ViewHolder
    {
        ImageView mImageViewType;
        TextView mTextName;
        TextView mTextSize;
        TextView mTextDate;
    }

    public ListFilesAdapter(Context context, List<FileItem> files,
            int resourceFile, int resourceFolder)
    {
        mContext = context;
        mFiles = files == null ? new ArrayList<FileItem>() : files;
        mResourceFile = resourceFile;
        mResourceFolder = resourceFolder;
    }

    public List<FileItem> getDatas()
    {
        return mFiles;
    }
    
    @Override
    public int getCount()
    {
        return mFiles.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mFiles.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        FileItem fileItem = (FileItem) getItem(position);
        if (fileItem == null)
        {
            return null;
        }

        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null)
        {
            convertView = ((Activity) mContext).getLayoutInflater().inflate(
                    R.layout.file_row, null);
            viewHolder.mImageViewType = (ImageView) convertView.findViewById(R.id.image_type);
            viewHolder.mTextName = (TextView) convertView.findViewById(R.id.name);
            viewHolder.mTextSize = (TextView) convertView.findViewById(R.id.size);
            viewHolder.mTextDate = (TextView) convertView.findViewById(R.id.date);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.mImageViewType.setImageResource(fileItem.isFile() ? mResourceFile : mResourceFolder);
        viewHolder.mTextName.setText(fileItem.getName());
        if(fileItem.getSize() < 0)
        {
            viewHolder.mTextSize.setVisibility(View.GONE);
        }
        else
        {
            viewHolder.mTextSize.setText(Utils.getSizeString(fileItem.getSize()));
            viewHolder.mTextSize.setVisibility(View.VISIBLE);
        }
        viewHolder.mTextDate.setText(fileItem.getDateString());

        return convertView;
    }

    public void setDatas(List<FileItem> files)
    {
        if (files == null)
        {
            return;
        }
        mFiles.clear();
        for (FileItem fileItem : files)
        {
            mFiles.add(fileItem);
        }
        notifyDataSetChanged();
    }
}
